package com.qf.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author 千锋健哥
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private EurekaClient eurekaClient;

    /**
     * 测试最原始的远程调用
     * @return
     */
    @GetMapping("/t1")
    public String test1() {

        //1. 访问Eureka, 根据被调用微服务的服务名称获取服务实例
        InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("QF-SEARCH", false);
        //2. 获取被调用服务的ip以及端口号地址
        String url = instanceInfo.getHomePageUrl();
        System.out.println("==========" + url);

        //3. 发送远程调用并返回结果, 直接通过ip地址访问, 不加ribbon负载均衡
        //String str = restTemplate.getForObject(url + "/search/test", String.class);

        //3. 发送远程调用并返回结果, 通过服务名访问, 有ribbon负载均衡效果
        String str = restTemplate.getForObject("http://QF-SEARCH/search/test", String.class);
        //4. 返回数据展示
        return str;

    }
}
